{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            GS10\n",
      "DATE            \n",
      "2020-06-01  0.73\n",
      "2020-07-01  0.62\n",
      "2020-08-01  0.65\n",
      "2020-09-01  0.68\n",
      "2020-10-01  0.79\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[*********************100%***********************]  1 of 1 completed"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               Open    High     Low    Close    Volume\n",
      "Date                                                  \n",
      "2025-02-28  236.950  242.09  230.20  241.840  56833360\n",
      "2025-02-27  239.410  242.46  237.06  237.300  41153639\n",
      "2025-02-26  244.330  244.98  239.13  240.360  44433564\n",
      "2025-02-25  248.000  250.00  244.91  247.040  48013272\n",
      "2025-02-24  244.925  248.86  244.42  247.172  51326396\n",
      "               Open     High      Low    Close    Volume\n",
      "Date                                                    \n",
      "2025-01-08  241.654  243.445  239.786  242.433  37670312\n",
      "2025-01-07  242.713  245.280  241.085  241.944  40900880\n",
      "2025-01-06  244.042  247.058  242.933  244.731  45095097\n",
      "2025-01-03  243.093  243.912  241.624  243.093  40288361\n",
      "2025-01-02  248.657  248.826  241.555  243.582  55802016\n",
      "Price             Close         High          Low         Open    Volume\n",
      "Ticker        000001.SS    000001.SS    000001.SS    000001.SS 000001.SS\n",
      "Date                                                                    \n",
      "2025-01-02  3262.561035  3351.721924  3242.086914  3347.938965    561400\n",
      "2025-01-03  3211.429932  3273.565918  3205.775879  3267.076904    517600\n",
      "2025-01-06  3206.923096  3219.488037  3185.462891  3209.782959    431000\n",
      "2025-01-07  3229.644043  3230.853027  3190.460938  3203.306885    409700\n",
      "2025-01-08  3230.167969  3246.291016  3175.725098  3218.857910    472900\n",
      "Price             Close         High          Low         Open    Volume\n",
      "Ticker        000001.SS    000001.SS    000001.SS    000001.SS 000001.SS\n",
      "Date                                                                    \n",
      "2025-02-24  3373.028076  3384.811035  3355.868896  3374.081055    598200\n",
      "2025-02-25  3346.040039  3369.558105  3337.844971  3345.653076    527600\n",
      "2025-02-26  3380.214111  3380.214111  3351.208984  3351.208984    554000\n",
      "2025-02-27  3388.062012  3388.781006  3353.571045  3378.448975    575800\n",
      "2025-02-28  3320.896973  3383.190918  3318.716064  3374.664062    568300\n",
      "DatetimeIndex(['2025-01-02', '2025-01-03', '2025-01-06', '2025-01-07',\n",
      "               '2025-01-08', '2025-01-09', '2025-01-10', '2025-01-13',\n",
      "               '2025-01-14', '2025-01-15', '2025-01-16', '2025-01-17',\n",
      "               '2025-01-20', '2025-01-21', '2025-01-22', '2025-01-23',\n",
      "               '2025-01-24', '2025-01-27', '2025-02-05', '2025-02-06',\n",
      "               '2025-02-07', '2025-02-10', '2025-02-11', '2025-02-12',\n",
      "               '2025-02-13', '2025-02-14', '2025-02-17', '2025-02-18',\n",
      "               '2025-02-19', '2025-02-20', '2025-02-21', '2025-02-24',\n",
      "               '2025-02-25', '2025-02-26', '2025-02-27', '2025-02-28'],\n",
      "              dtype='datetime64[ns]', name='Date', freq=None)\n",
      "MultiIndex([( 'Close', '000001.SS'),\n",
      "            (  'High', '000001.SS'),\n",
      "            (   'Low', '000001.SS'),\n",
      "            (  'Open', '000001.SS'),\n",
      "            ('Volume', '000001.SS')],\n",
      "           names=['Price', 'Ticker'])\n",
      "Price         Close         High          Low         Open         Volume\n",
      "Ticker    000001.SS    000001.SS    000001.SS    000001.SS      000001.SS\n",
      "count     36.000000    36.000000    36.000000    36.000000      36.000000\n",
      "mean    3280.582058  3300.454332  3259.610731  3280.405904  482483.333333\n",
      "std       64.965211    60.665482    64.670967    64.532126   78546.995941\n",
      "min     3160.754883  3172.699951  3140.978027  3148.825928  364800.000000\n",
      "25%     3230.034058  3253.953064  3214.300171  3226.715210  404450.000000\n",
      "50%     3257.593506  3273.979004  3240.104004  3268.151978  487900.000000\n",
      "75%     3346.126282  3355.947937  3321.068054  3346.137085  555850.000000\n",
      "max     3388.062012  3388.781006  3355.868896  3378.448975  608200.000000\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "DatetimeIndex: 36 entries, 2025-01-02 to 2025-02-28\n",
      "Data columns (total 5 columns):\n",
      " #   Column               Non-Null Count  Dtype  \n",
      "---  ------               --------------  -----  \n",
      " 0   (Close, 000001.SS)   36 non-null     float64\n",
      " 1   (High, 000001.SS)    36 non-null     float64\n",
      " 2   (Low, 000001.SS)     36 non-null     float64\n",
      " 3   (Open, 000001.SS)    36 non-null     float64\n",
      " 4   (Volume, 000001.SS)  36 non-null     int64  \n",
      "dtypes: float64(4), int64(1)\n",
      "memory usage: 1.7 KB\n",
      "None\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "import pandas_datareader.data as web\n",
    "import datetime\n",
    "import yfinance as yf\n",
    "gs10 = web.get_data_fred('GS10')\n",
    "print(gs10.head())\n",
    "#上证指数行情数据\n",
    "df_stockload = web.DataReader(\"AAPL\", \"stooq\",datetime.datetime(2025,1,1),datetime.datetime(2025,3,1))\n",
    "print(df_stockload.head()) #前几行\n",
    "print(df_stockload.tail()) #末尾几行\n",
    "data = yf.download('000001.SS', start = '2025-01-01', end='2025-03-01')\n",
    "print(data.head()) #前几行\n",
    "print(data.tail()) #末尾几行\n",
    "print(data.index) #行索引\n",
    "print(data.columns) #列索引\n",
    "print(data.describe()) # 统计\n",
    "print(data.info()) # 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
      "0  000001.SZ   20250606  11.70  11.79  11.68  11.70      11.67    0.03   \n",
      "1  000001.SZ   20250605  11.88  11.91  11.66  11.67      11.84   -0.17   \n",
      "2  000001.SZ   20250604  11.82  11.88  11.78  11.84      11.81    0.03   \n",
      "3  000001.SZ   20250603  11.54  11.91  11.53  11.81      11.56    0.25   \n",
      "\n",
      "   pct_chg         vol       amount  \n",
      "0   0.2571   682352.51   799900.024  \n",
      "1  -1.4358  1166803.49  1369212.336  \n",
      "2   0.2540  1167959.50  1383019.682  \n",
      "3   2.1626  2192479.66  2580962.601  \n"
     ]
    },
    {
     "ename": "Exception",
     "evalue": "抱歉，您每分钟最多访问该接口1次，权限的具体详情访问：https://tushare.pro/document/1?doc_id=108。",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mException\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 6\u001b[0m\n\u001b[0;32m      4\u001b[0m df_daily \u001b[38;5;241m=\u001b[39m pro\u001b[38;5;241m.\u001b[39mdaily(ts_code\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m000001.SZ\u001b[39m\u001b[38;5;124m'\u001b[39m,start_date\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m20250601\u001b[39m\u001b[38;5;124m'\u001b[39m,end_date \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m20250606\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28mprint\u001b[39m(df_daily)\n\u001b[1;32m----> 6\u001b[0m df \u001b[38;5;241m=\u001b[39m \u001b[43mpro\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstock_basic\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexchange\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlist_status\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mL\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Users\\yibozhang\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\tushare\\pro\\client.py:45\u001b[0m, in \u001b[0;36mDataApi.query\u001b[1;34m(self, api_name, fields, **kwargs)\u001b[0m\n\u001b[0;32m     43\u001b[0m result \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mloads(res\u001b[38;5;241m.\u001b[39mtext)\n\u001b[0;32m     44\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m result[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcode\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m---> 45\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(result[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmsg\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[0;32m     46\u001b[0m data \u001b[38;5;241m=\u001b[39m result[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdata\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[0;32m     47\u001b[0m columns \u001b[38;5;241m=\u001b[39m data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfields\u001b[39m\u001b[38;5;124m'\u001b[39m]\n",
      "\u001b[1;31mException\u001b[0m: 抱歉，您每分钟最多访问该接口1次，权限的具体详情访问：https://tushare.pro/document/1?doc_id=108。"
     ]
    }
   ],
   "source": [
    "import tushare as ts\n",
    "ts.set_token('e67298b15a0af8266c6f0346df61429a94bd13c591e3c3711022daae')\n",
    "pro = ts.pro_api()\n",
    "df_daily = pro.daily(ts_code='000001.SZ',start_date='20250601',end_date = '20250606')\n",
    "print(df_daily)\n",
    "df = pro.stock_basic(exchange='', list_status='L')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-05-23\n",
      "login success!\n",
      "logout success!\n",
      "             High    Low   Open  Close     Volume\n",
      "Date                                             \n",
      "2025-05-19  10.14   9.97  10.03  10.09  218636.50\n",
      "2025-05-20  10.18  10.03  10.09  10.08  241594.81\n",
      "2025-05-21  10.13  10.04  10.07  10.05  194776.55\n",
      "2025-05-22  10.09  10.00  10.04  10.00  165329.43\n",
      "2025-05-23  10.09   9.92  10.00   9.93  286912.00\n"
     ]
    }
   ],
   "source": [
    "import baostock as bs\n",
    "import pandas as pd\n",
    "import datetime\n",
    "# 登陆系统\n",
    "def bs_k_data_stock(code_val='sz.000651',start_val = '2025-01-01', end_val='2025-05-01',freq_val='d',adjust_val='3'):\n",
    "    lg = bs.login()\n",
    "    # 获取历史行情数据\n",
    "    fields = \"date,open,high,low,close,volume\"\n",
    "    df_bs = bs.query_history_k_data_plus(code_val, fields, start_date=start_val, end_date=end_val,frequency=freq_val ,adjustflag=adjust_val)\n",
    "    #日K线，3，默认不赋权，1 后复权，2 前复权\n",
    "    data_list= []\n",
    "    while (df_bs.error_code == '0') & df_bs.next():\n",
    "        #获取一条记录，将记录合并在一起\n",
    "        data_list.append(df_bs.get_row_data())\n",
    "    result = pd.DataFrame(data_list, columns=df_bs.fields)\n",
    "\n",
    "    result.close = result.close.astype('float64')\n",
    "    result.open = result.open.astype('float64')\n",
    "    result.low = result.low.astype('float64')\n",
    "    result.high = result.high.astype('float64')\n",
    "    result.volume = result.volume.astype('float64')\n",
    "    result.volume = result.volume/100 #单位转换:股-手\n",
    "    result.date = pd.DatetimeIndex(result.date)\n",
    "    result.set_index(\"date\", drop=True, inplace= True)\n",
    "    result.index = result.index.set_names('Date')\n",
    "\n",
    "    recon_data = {'High' : result.high, 'Low': result.low, 'Open': result.open, 'Close':result.close, 'Volume': result.volume}\n",
    "    df_recon = pd.DataFrame(recon_data)\n",
    "\n",
    "    #退出系统\n",
    "    bs.logout()\n",
    "    return df_recon\n",
    "\n",
    "ymd = datetime.datetime.now().strftime(\"%Y-%m-%d\")\n",
    "print (ymd)\n",
    "data = bs_k_data_stock('sz.000528','2025-01-01',ymd,'d','3')\n",
    "print(data.tail())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'指数': {'上证成指': 'sh.000001', '深证成指': 'ez.399001', '沪深300': 'sz.000300', '创业板指': 'sz.399006', '上证50': 'sh.000016', '中证500': 'sh.000905', '中小板指': 'sz.399005', '上证180': 'sh.000010'}}, {'股票': {'格力电器': '000641.SZ', '平安银行': '000001.SZ', '同花顺': '300033.SZ', '贵州茅台': '600519.SH', '浙大网新': '600797.SH'}}]\n",
      "<class 'list'>\n",
      "[{\"\\u6307\\u6570\": {\"\\u4e0a\\u8bc1\\u6210\\u6307\": \"sh.000001\", \"\\u6df1\\u8bc1\\u6210\\u6307\": \"ez.399001\", \"\\u6caa\\u6df1300\": \"sz.000300\", \"\\u521b\\u4e1a\\u677f\\u6307\": \"sz.399006\", \"\\u4e0a\\u8bc150\": \"sh.000016\", \"\\u4e2d\\u8bc1500\": \"sh.000905\", \"\\u4e2d\\u5c0f\\u677f\\u6307\": \"sz.399005\", \"\\u4e0a\\u8bc1180\": \"sh.000010\"}}, {\"\\u80a1\\u7968\": {\"\\u683c\\u529b\\u7535\\u5668\": \"000641.SZ\", \"\\u5e73\\u5b89\\u94f6\\u884c\": \"000001.SZ\", \"\\u540c\\u82b1\\u987a\": \"300033.SZ\", \"\\u8d35\\u5dde\\u8305\\u53f0\": \"600519.SH\", \"\\u6d59\\u5927\\u7f51\\u65b0\": \"600797.SH\"}}]\n"
     ]
    }
   ],
   "source": [
    "# python股票池\n",
    "import json\n",
    "stock_index=[{'指数':\n",
    "              {'上证成指': 'sh.000001',\n",
    "               '深证成指':'ez.399001',\n",
    "               '沪深300':'sz.000300',\n",
    "               '创业板指':'sz.399006',\n",
    "               '上证50':'sh.000016',\n",
    "               '中证500':'sh.000905',\n",
    "               '中小板指':'sz.399005',\n",
    "               '上证180':'sh.000010'}},\n",
    "               {'股票':\n",
    "                {\n",
    "                    '格力电器':'000641.SZ',\n",
    "                    '平安银行':'000001.SZ',\n",
    "                    '同花顺':'300033.SZ',\n",
    "                    '贵州茅台':'600519.SH',\n",
    "                    '浙大网新':'600797.SH'\n",
    "                }}]\n",
    "print(stock_index)\n",
    "print(type(stock_index))\n",
    "# dumps: 将数据转换成字符串\n",
    "json_str = json.dumps(stock_index)\n",
    "print(json_str)\n",
    "with open(\"stock_pool.json\", \"w\", encoding='utf-8') as f:\n",
    "    json.dump(stock_index, f, ensure_ascii=False, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "right code is 000641.SZ\n",
      "right code is 000001.SZ\n",
      "right code is 300033.SZ\n",
      "right code is 600519.SH\n",
      "right code is 600797.SH\n",
      "right code is 600618.SH\n",
      "right code is 002335.SZ\n",
      "right code is 002507.SZ\n",
      "right code is 600111.SH\n",
      "right code is 000528.SZ\n",
      "right code is 600637.SH\n",
      "right code is 603099.SH\n",
      "right code is 601595.SH\n",
      "Time of 0 used: 1.552087299991399 \n",
      "<class 'list'>\n",
      "Time of 0 used: 0.45461359999899287 \n"
     ]
    }
   ],
   "source": [
    "import tushare as ts\n",
    "import time\n",
    "from concurrent.futures import ThreadPoolExecutor\n",
    "from multiprocessing import Pool\n",
    "import json\n",
    "#定义测试代码执行时间的装饰器--三阶\n",
    "def timeit_test(number=3, repeat=3):\n",
    "    def decorator(func):\n",
    "        def wrapper(*args, **kwargs):\n",
    "            for i in range(repeat):\n",
    "                start = time.perf_counter()\n",
    "                for _ in range(number):\n",
    "                    func(*args, **kwargs)\n",
    "                elapsed = (time.perf_counter() - start)\n",
    "                print('Time of {} used: {} '.format(i, elapsed))\n",
    "        return wrapper\n",
    "    return decorator\n",
    "def json_to_str():\n",
    "    #load 将文件中的字符串变换为数据类型\n",
    "    with open(\"stock_pool.json\", \"r\", encoding='utf-8') as f:\n",
    "        stock_index = json.load(f)\n",
    "    # print(stock_index)\n",
    "    return stock_index\n",
    "\n",
    "def pro_daily_stock(code, start_date,end_date):\n",
    "    ts.set_token('e67298b15a0af8266c6f0346df61429a94bd13c591e3c3711022daae')\n",
    "    pro = ts.pro_api()\n",
    "    df_daily = pro.daily(ts_code= code,start_date= start_date,end_date = end_date)\n",
    "\n",
    "\n",
    "@timeit_test(number =1, repeat =1)\n",
    "# 获取股票数据\n",
    "def get_daily_data(start='20240101',end='20250101'):\n",
    "    stock_index = json_to_str()\n",
    "    stock_code = list(list(stock_index[1].values())[0].values())\n",
    "    for code in stock_code:\n",
    "        try:\n",
    "            df_data = pro_daily_stock(code, start, end)\n",
    "            print(\"right code is %s\" % code)\n",
    "        except:\n",
    "            print(\"errror code is %s\" % code)\n",
    "\n",
    "def map_fun(code, start='20240101', end='20250101'):\n",
    "    try:\n",
    "        df_data = pro_daily_stock(code, start, end)\n",
    "    except:\n",
    "        print(\"error code is %s\" % code)\n",
    "\n",
    "# 多线程\n",
    "@timeit_test(number=1, repeat=1)\n",
    "def get_daily_thread():\n",
    "    stock_index = json_to_str()\n",
    "    stock_code = list(list(stock_index[1].values())[0].values())\n",
    "    print(type(stock_code))\n",
    "   \n",
    "    #print(itr_arg)\n",
    "    with ThreadPoolExecutor(max_workers=4) as executor:\n",
    "        result = executor.map(map_fun, stock_code[0:13])\n",
    "\n",
    "@timeit_test(number=1, repeat=1)\n",
    "def get_daily_multi():\n",
    "    stock_index = json_to_str()\n",
    "    stock_code = list(list(stock_index[1].values())[0].values())\n",
    " \n",
    "\n",
    "    pool = Pool(1) # 创建拥有4个进程数量的进程池\n",
    "    pool.map(map_fun, stock_code[0:13])\n",
    "    pool.close() # 关闭进程池，不再接受新的进程\n",
    "    pool.join() # 主进程阻塞等待子进程的退出\n",
    "\n",
    "get_daily_data(start='20240101',end='20250101')\n",
    "get_daily_thread()\n",
    "get_daily_multi()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
